<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="100px">

            <el-form-item label="MAC地址:" prop="jobName">
                <el-input v-model="queryParams.deviceId" placeholder="请输入网关MAC地址" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="所在区域:" prop="jobName">
                <el-select v-model="queryParams.areaId" placeholder="请选择设备所在区域" clearable :style="{ width: '100%' }"
                    filterable>
                    <el-option v-for="(item, index) in areaInfoList" :key="index" :label="item.menuName"
                        :value="item.menuId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                    @click="handleUpdate">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete">删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="设备mac地址" width="140" align="center" prop="deviceId" />
            <el-table-column prop="imUrl" label="展示图片" width="90" align="center">
                <template slot-scope="scope">
                    <!-- http://business.alm-iot.com/api/file/438528be5cfbe3014f9391a1d6e2f65d.png -->
                    <el-button type="danger" plain size="mini" v-if="scope.row.imUrl"
                        @click="showImgDig(scope.row)">查看图片</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="devName" label="设备别名" width="120" align="center">
            </el-table-column>
            <el-table-column prop="typeName" label="设备类型" width="120" align="center">
            </el-table-column>
            <el-table-column prop="devModelTypeName" label="设备型号" width="120" align="center"
                :show-overflow-tooltip="true">
            </el-table-column>
            <!-- <el-table-column prop="devVersion" label="版本信息" width="120" align="center" :show-overflow-tooltip="true">
            </el-table-column> -->

            <el-table-column prop="sectionName" label="归属公司" width="120" align="center">
            </el-table-column>
            <el-table-column prop="customerName" label="归属人" width="120" align="center">
            </el-table-column>
            <el-table-column prop="areaName" label="归属区域" width="120" align="center">
            </el-table-column>
            <el-table-column prop="remark" label="备注" width="120" align="center" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column prop="addTime" label="添加时间" width="250" :formatter="parseTime" align="center"
                :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="200" align="center">
                <template slot-scope="scope" v-if="scope.row.optBut ? false : true">
                    <el-button size="mini" type="text" icon="el-icon-connection"
                        @click="showAbatar(scope.row)">更改图片</el-button>
                    <el-button size="mini" type="text" icon="el-icon-refresh"
                        @click="showRefreshImg(scope.row)">图片刷新</el-button>
                </template>
            </el-table-column>
        </el-table>


        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改【请填写功能名称】对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="标签mac" prop="deviceId">
                    <el-input v-model="form.deviceId" :disabled="upisDisabledByMac" placeholder="请输入标签mac" />
                </el-form-item>
                <el-form-item label="设备别名" prop="devName">
                    <el-input v-model="form.devName" placeholder="请输入设备别名" />
                </el-form-item>
                <el-form-item label="设备型号">
                    <el-select @change="$forceUpdate()" v-model="form.devModelType">
                        <el-option v-for="item in modelList" :key="item.id" :value="item.id"
                            :label="item.name"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="归属区域">
                    <el-select @change="$forceUpdate()" v-model="form.regionId">
                        <el-option v-for="item in areaInfoListByEdit" :key="item.menuId" :value="item.menuId"
                            :label="item.menuName"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <el-dialog title="修改图片" :visible.sync="updateAbatarShow" width="500px" append-to-body>
            <div class="text-center">
                <lableAvatar :imgdata="imgdata" />
            </div>
        </el-dialog>
        <el-dialog title="查看图片" :visible="dialogVisible" width="600px" height="600px" @close="dialogVisible = false">
            <!-- 在对话框中展示图片 -->
            <div style="display: flex; justify-content: center; align-items: center; height: 100%;">
                <img v-bind:src="imgdata.imgUrl" :width="imgdata.widht" :height="imgdata.heght" alt="展示图片">
            </div>
        </el-dialog>
        <el-dialog title="标签刷新" :visible.sync="imgRefreshDig" width="30%" :close-on-click-modal="false">
            <el-form label-width="100px">
                <el-form-item label="标签编号">
                    <el-input v-model="refreshImg.devid" placeholder="请输入标签MAC" disabled></el-input>
                </el-form-item>
                <el-form-item label="连接网关">
                    <el-input v-model="refreshImg.gwMac" placeholder="请输入连接的网关MAC"></el-input>
                </el-form-item>
                <el-form-item label="偏移X">
                    <el-input v-model="refreshImg.skewX" placeholder="释义: X轴偏移量 16进制"></el-input>
                </el-form-item>
                <el-form-item label="偏移Y">
                    <el-input v-model="refreshImg.skewY" placeholder="释义: Y轴偏移量 16进制"></el-input>
                </el-form-item>
                <el-form-item label="图片类型">
                    <el-select v-model="refreshImg.imgType" placeholder="图片类型" >
                        <el-option label="前景图片" value="02"></el-option>
                        <el-option label="背景图片" value="01"></el-option>
                    </el-select>
                </el-form-item>
                <!-- <el-form-item label="图片类型">

                    <el-input v-model="refreshImg.imgType" placeholder="请输入图片类型"></el-input>
                </el-form-item> -->
                <el-form-item label="图片ID">
                    <el-input v-model="refreshImg.imgNo" placeholder="请输入图片id"></el-input>
                </el-form-item>
                <el-form-item label="图片大小规格">
                    <el-input v-model="refreshImg.sizeType" placeholder="请输入图片id"></el-input>
                </el-form-item>
                <el-form-item label="图层数">
                    <el-input v-model="refreshImg.layerNumber" placeholder="请输入图片id"></el-input>
                </el-form-item>
                <el-form-item label="刷新进度">
                    <el-progress color="#8e71c7" :percentage="percentageJindu"></el-progress>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="hideRefreshImg()">取消</el-button>
                <el-button type="primary" @click="sendRefreshImg()" :loading="sendRefreshShow">刷新</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

import { getDevInfoList, getModelListByType, submitEditForm, upLevelSubmit } from "@/api/alm/dev-debug/dev-network-manage/dev-network-manage";
import Crontab from '@/components/Crontab'
import { parseTime } from "@/utils/ruoyi";
import lableAvatar from "./labelAvatar";
import { listInfo, getInfo, delInfo, addInfo, updateInfo, queryImgInfoById, refreshImg, querschedule } from "@/api/alm/getway-manager/getway-lable/lable";
import { getAreaList, getAreaInfo } from "@/api/alm/statistical-export/area-manage/area-manage";
export default {
    name: "Info",
    components: { lableAvatar },
    data() {
        return {
            imgdata: {

            },
            refreshImg: {
                devid: '',
                gwMac: 'C58FE0620DC5',
                skewY: 300,
                skewX: 0,
                imgType: '01',
                imgNo: '01',
                layerNumber: '3',
                modelType: '',
                sizeType: 1,
            },
            sendRefreshShow: false,
            percentageJindu: 0,
            dialogVisible: false,
            updateAbatarShow: false,
            // 遮罩层
            loading: true,
            imgTime: null,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 【请填写功能名称】表格数据
            infoList: [],
            // 弹出层标题
            title: "",
            countQuery: 0,
            upisDisabledByMac: false,
            // 是否显示弹出层
            open: false,
            areaInfoList: [],
            labelAvatar: [],
            imgRefreshDig: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                deviceId: null,
                devName: null,
                devType: null,
                devModelType: null,
                sectionId: null,
                customerId: null,
                regionId: null,
                addTime: null,
                actTime: null,
                routerMac: null,
                devVersion: null,
                devid: undefined,
                areaId: '',
            },
            areaInfoListByEdit: [],
            modelList: [],
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        this.getList();
        this.getAreaInfoList();
    },
    methods: {
        showRefreshImg(row) {
            this.imgRefreshDig = true;
            this.refreshImg.devid = row.deviceId;
            this.refreshImg.modelType = row.devModelType;
        },
        sendRefreshImg() {
            this.percentageJindu = 0;
            this.sendRefreshShow = true;
            refreshImg(this.refreshImg).then(response => {
                this.sendRefreshShow = false;
                if (response.code == 200) {
                    this.percentageJindu = 1;
                    this.querschedules(this.refreshImg.gwMac);
                } else {
                    this.$message.error("发送失败");
                }
            })
        },
        querschedules(gwmac) {
            querschedule(gwmac).then(response => {
                if (response.data.isErrer) {
                    console.log(12);
                    this.$message.error(response.data.msg);
                    clearInterval(this.imgTime);
                    this.imgTime = null;
                    this.countQuery = 0;
                } else {
                    this.countQuery += 1;
                    console.log(this.countQuery);
                    if (this.countQuery > 30) {
                        clearInterval(this.imgTime);
                        this.imgTime = null;
                        this.countQuery = 0;
                    }
                    this.percentageJindu = response.data.schedule == null ? 0 : response.data.schedule;
                    if (this.imgTime == null) {
                        this.imgTime = setInterval(() => {
                            this.querschedules(gwmac);
                        }, 2000);
                    }
                    if (response.data.schedule == 100) {
                        clearInterval(this.imgTime);
                        this.imgTime = null;
                        this.countQuery = 0;
                    }
                }
            });
        },
        hideRefreshImg() {
            this.imgRefreshDig = false;
            if (this.imgTime != null) {
                clearInterval(this.imgTime);
                this.imgTime = null;
                this.countQuery = 0;
            }
        },
        showAbatar(row) {
            queryImgInfoById(row.id).then(response => {
                if (response.code == 200) {
                    this.imgdata = response.data;
                    console.log(this.imgdata);
                    this.updateAbatarShow = true;
                } else {
                    this.$message.error("未绑定设备类型或未设置该类型的图片长宽");
                }
            })
        },
        showImgDig(row) {
            queryImgInfoById(row.id).then(response => {
                if (response.code == 200) {
                    this.imgdata = response.data;
                    console.log(this.imgdata);
                    this.dialogVisible = true;
                } else {
                    this.$message.error("未绑定设备类型或未设置该类型的图片长宽");
                }
            })
        },
        getModelList() {
            getModelListByType({ type: 3 }).then(response => {
                this.modelList = response.rows;
            })
        },
        /** 查询【请填写功能名称】列表 */
        getList() {
            this.loading = true;
            listInfo(this.queryParams).then(response => {
                this.infoList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                deviceId: null,
                devName: null,
                devType: null,
                devModelType: null,
                sectionId: null,
                customerId: null,
                regionId: null,
                addTime: null,
                actTime: null,
                routerMac: null,
                devVersion: null,
                remark: null
            };
            this.resetForm("form");
        },
        getAreaInfoList() {
            getAreaList({}).then(response => {
                this.areaInfoList = response.data;
                this.areaInfoListByEdit = response.data
            })
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "录入标签信息";
            this.getModelList();
            this.upisDisabledByMac = false;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.getModelList();
            this.upisDisabledByMac = true;
            this.reset();
            const id = row.id || this.ids
            getInfo(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改标签信息";
            });
        },
        parseTime(params) {
            return parseTime(params.addTime);
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateInfo(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addInfo(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {

            this.$modal.confirm('是否确认删除该数据？').then(function () {
                return delInfo(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/info/export', {
                ...this.queryParams
            }, `info_${new Date().getTime()}.xlsx`)
        }
    }

};
</script>